using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;
using Services;

namespace Sudoku.Menu
{
	public class BannerComponent : ExtendedGameComponent
	{
		private Vector2 position;
		private String fullString;
		private uint numberOfChars;
		private SpriteFont font;
		private int delay;
		private Color color;

		private int lastUpdate;
		private String currentString;
		private int begin;

		public BannerComponent(Game game)
			: base(game)
		{
			position = new Vector2(0, 0);
			fullString = "";
			currentString = "";
			numberOfChars = 10;
			font = fontService.Font(FontType.Delicious26Bold);
			color = Color.White;
			delay = 1000;

			lastUpdate = 0;
			currentString = "";
			begin = 0;
		}

		public override void Update(GameTime gameTime)
		{
			lastUpdate += gameTime.ElapsedGameTime.Milliseconds;

			if (lastUpdate > delay)
			{
				lastUpdate = 0;
				ScrollText();
			}

			base.Update(gameTime);
		}

		private void ScrollText()
		{
			currentString = "";

			int j = begin;
			for (int i = 0; i < numberOfChars; ++i)
			{
				j = j < fullString.Length - 1? j + 1 : 0;
				currentString += fullString[j];
			}

			begin = begin < fullString.Length ? begin + 1 : 0;
		}

		public override void Draw(GameTime gameTime)
		{
			spriteBatch.Begin();
			spriteBatch.DrawString(font, currentString, position, color);
			spriteBatch.End();

			base.Draw(gameTime);
		}

		#region Get and Set Methods

		public Vector2 Position
		{
			get { return position; }
			set { position = value; }
		}

		public String Message
		{
			get { return fullString; }
			set { fullString = value; }
		}

		public uint NumberOfChars
		{
			get { return numberOfChars; }
			set { numberOfChars = value; }
		}

		public int ScrollSpeed
		{
			get { return delay; }
			set { delay = value; }
		}

		public SpriteFont Font
		{
			get { return font; }
			set { font = value; }
		}

		public Color Color
		{
			get { return color; }
			set { color = value; }
		}

		#endregion
	}
}
